// localStorage
export const setLocal = (key: string, value: any) => {
  if (key && (value || value === 0)) {
    const type = typeof value
    let val = JSON.stringify(value)
    if (type === 'string') {
      val = value
    }
    localStorage.setItem(key, val)
  } else {
    throw new Error(`Missing 'key' or 'value'`)
  }
}

export const getLocal = (key: string) => {
  let val = localStorage.getItem(key)
  if (val) {
    try {
      val = JSON.parse(val)
    } catch (e) {}
  }
  return val
}

export const removeLocal = (key: string) => {
  localStorage.removeItem(key)
}

// sessionStorage
export const setSession = (key: string, value: any) => {
  if (key && (value || value === 0)) {
    const type = typeof value
    let val = JSON.stringify(value)
    if (type === 'string') {
      val = value
    }
    sessionStorage.setItem(key, val)
  } else {
    throw new Error(`Missing 'key' or 'value'`)
  }
}
export const getSession = (key: string) => {
  const val = sessionStorage.getItem(key)
  if (val) {
    try {
      return JSON.parse(val)
    } catch (e) {}
  }
  return val
}

export const RemoveSession = (key: string) => {
  sessionStorage.removeItem(key)
}

/**
 * 打开全屏模式
 */
export function openFullscreen(elem: any) {
  if (!elem) {
    elem = document.body
  }
  if (elem.requestFullscreen) {
    elem.requestFullscreen()
  } else if (elem.mozRequestFullScreen) {
    /* Firefox */
    elem.mozRequestFullScreen()
  } else if (elem.webkitRequestFullscreen) {
    /* Chrome, Safari & Opera */
    elem.webkitRequestFullscreen()
  } else if (elem.msRequestFullscreen) {
    /* IE/Edge */
    elem.msRequestFullscreen()
  }
}
